﻿namespace BlackHole
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using BlackHole.DAO;
    using NPOI.HSSF.UserModel;

    /// <summary>
    /// 产品统计页面
    /// </summary>
    public partial class Overview : System.Web.UI.Page
    {
        /// <summary>
        /// 服务端渲染控件 保留
        /// </summary>
        /// <param name="control">保留</param>
        public override void VerifyRenderingInServerForm(Control control)
        {
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.RenderUI();
            }
        }

        /// <summary>
        /// 维修记录导出到excel
        /// </summary>
        protected void ExportToExcel()
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("content-disposition", "attachment;filename=MyFiles.xls");
            Response.Charset = string.Empty;
            this.EnableViewState = false;

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

            this.dataRecords.RenderControl(htw);

            Response.Write(sw.ToString());
            Response.End();
        }

        /// <summary>
        /// 导出到excel
        /// </summary>
        /// <param name="ms">内存流</param>
        /// <param name="fileName">文件名</param>
        protected void ExportToExcel(MemoryStream ms, string fileName)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ".xls");
            Response.Charset = string.Empty;
            this.EnableViewState = false;

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

            Response.OutputStream.Write(ms.ToArray(), 0, ms.ToArray().Length);
            Response.End();
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        protected void RenderUI()
        {
            this.comboDistricts.Items.Add(new ListItem("(全部)", "0"));
            foreach (District d in Utility.GetAllDistricts())
            {
                this.comboDistricts.Items.Add(new ListItem(d.DistrictName, d.Id.ToString()));
            }

            this.comboDevices.Items.Add(new ListItem("(全部)", "0"));
            foreach (Device d in Utility.GetAllDevices())
            {
                this.comboDevices.Items.Add(new ListItem(d.DeviceName, d.Id.ToString()));
            }
        }

        /// <summary>
        /// 事件 更改区县
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ComboDistricts_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.comboOwners.Items.Clear();
            this.comboOwners.Items.Add(new ListItem("(全部)", "0"));
            foreach (Owner o in Utility.GetOwnersByDistrictId(int.Parse(this.comboDistricts.SelectedValue)))
            {
                this.comboOwners.Items.Add(new ListItem(o.OwnerName, o.Id.ToString()));
            }
        }

        /// <summary>
        /// 事件 更改设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ComboDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.comboFaults.Items.Clear();
            this.comboFaults.Items.Add(new ListItem("(全部)", "0"));
            foreach (Fault f in Utility.GetFaultsByDeviceId(int.Parse(this.comboDevices.SelectedValue)))
            {
                this.comboFaults.Items.Add(new ListItem(f.FaultCode + f.FaultName, f.Id.ToString()));
            }
        }

        /// <summary>
        /// 事件 导出维修记录到excel按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnExport_Click(object sender, EventArgs e)
        {
            this.ExportToExcel();
        }

        /// <summary>
        /// 事件 导出产品信息到excel按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnExportProductDetails_Click(object sender, EventArgs e)
        {
            List<Product> products = Utility.GetAllProductsDetails();
            HSSFWorkbook workbook = new HSSFWorkbook();

            var sheet = workbook.CreateSheet("自古枪兵幸运E");
            sheet.CreateRow(0);

            sheet.GetRow(0).CreateCell(0).SetCellValue("设备名称");
            sheet.GetRow(0).CreateCell(1).SetCellValue("区县");
            sheet.GetRow(0).CreateCell(2).SetCellValue("网点");
            sheet.GetRow(0).CreateCell(3).SetCellValue("状态");
            sheet.GetRow(0).CreateCell(4).SetCellValue("序列号");
            sheet.GetRow(0).CreateCell(5).SetCellValue("Psam号");
            sheet.GetRow(0).CreateCell(6).SetCellValue("固件版本");
            sheet.GetRow(0).CreateCell(7).SetCellValue("维修次数");

            int i = 1;
            foreach (var p in products)
            {
                sheet.CreateRow(i);
                sheet.GetRow(i).CreateCell(0).SetCellValue(p.ProductDeviceName);
                sheet.GetRow(i).CreateCell(1).SetCellValue(p.ProductDistrictName);
                sheet.GetRow(i).CreateCell(2).SetCellValue(p.ProductOwnerName);
                sheet.GetRow(i).CreateCell(3).SetCellValue(p.StatusName);
                sheet.GetRow(i).CreateCell(4).SetCellValue(p.SerialNO);
                sheet.GetRow(i).CreateCell(5).SetCellValue(p.PsamNO);
                sheet.GetRow(i).CreateCell(6).SetCellValue(p.RomVersion);
                sheet.GetRow(i).CreateCell(7).SetCellValue(p.RepairCycle.ToString());
                i++;
            }

            MemoryStream ms = new MemoryStream();
            workbook.Write(ms);
            ExportToExcel(ms, "products");
            ms.Close();
        }
    }
}